<%-- 
    Document   : NumGuess
    Created on : Feb 22, 2012, 2:07:10 PM
    Author     : jdepena
--%>
<%@page import="java.util.Random"%>
<%! 

private String _message = "Guess a number between 1 and 10: ";
private int _randomNumber = 0;
private String _number = "";
private Random _random;
HttpSession _session; 

// Override the jspInit method which is invoked when the JSP page is initialized
public void jspInit() 
{    
    _random = new Random();
}

%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>De Pena - HMK#7</title>
    </head>
    <body>
       <%
         _session = request.getSession();
        if (_session.getAttribute("number") == null)
        {
            _randomNumber = _random.nextInt(9) + 1;
            _session.setAttribute("number", _randomNumber);
        }
        else
        {
            _randomNumber = Integer.parseInt(_session.getAttribute("number").toString());
        }
         
        _number = request.getParameter("number");
            
        try
        {
            Integer guess = Integer.parseInt(_number);
            if (guess < _randomNumber)
            {
                out.println("<b>Too low!</b><br/><br/>");
            }
            else if (guess > _randomNumber)
            {
                out.println("<b>Too high!</b><br/><br/>");
            }
            else if (guess == _randomNumber)
            {
                out.println("<b>Correct!</b><br/><br/>");
                    _randomNumber = _random.nextInt(9) + 1;
                    _session.setAttribute("number", _randomNumber);
            }
        }
        catch (NumberFormatException nfe)
        {
            out.println("<b>Please enter a number!</b><br/><br/>");
        }
        
       %>
       <form method='post'>
           <% out.println(_message); %>
            <input type='text' name='number' size='2'/><br />
            <input type='submit' value='Submit' style='width: 80px; height: 30px'/>
       </from>
    </body>
</html>
